var file = require('../file.js').file;
var dir = require('../dir.js').dir;
var db = require('../filedb.js').filedb;
var api = require('../commapi.js');

//-测试环境设置-
var prjpath = "./temp/prjtest";
var dbScale = 1;			//-数据库规模-

function file_open()
{
	file.open(prjpath);
}

function file_close()
{
	file.save();
	file.close();
}

function dir_open()
{
	dir.setDbScale(dbScale);
	dir.open(prjpath);
}

function dir_close()
{
	dir.save();
	dir.close();
}

function db_open()
{
	db.setDbScale(dbScale);
	db.open(prjpath);
}

function db_close()
{
	db.save();
	db.close();
}

//-----------------------------------
//			test API
//-----------------------------------
//-文件目录是否存在-
function test_isFileExist()
{
	var strPath = prjpath;
	var bExist = api.isFileExist(strPath);
	console.log(strPath + " exist :" + bExist);
}

//-创建某个父目录下的子目录-
function test_createDir()
{
	var strRoot = ".";
	var strPath = "tmp/t2";
	var strAll = strRoot+"/"+strPath;
	api.createDir(strRoot, strPath);
	var bExist = api.isFileExist(strAll);
	console.log(strAll  + " exist :" + bExist);
}

//-根据最终目录形式一路创建到底-
function test_createSubDir()
{
	var strPath = "./tmp/t1/t3";
	api.createSubDir(strPath);
	var bExist = api.isFileExist(strPath);
	console.log(strPath  + " exist :" + bExist);
}

//-----------------------------------
//			test File index
//-----------------------------------
//-新建文件索引库-
function test_file_new()
{	
file_open();
	for(var i=1; i<=100; i++)
		file.fAddRcdInfo("src", i, "txt", "./src"+i+".txt");
file_close();
}

//-打开文件索引库-
function test_file_open()
{
file_open();
	console.log(file.fGet("src", 5, "txt"));
file_close();
}


//-----------------------------------
//			test Dir index
//-----------------------------------
//-新建目录索引库-
function test_dir_new()
{
dir_open();	
	for(var i=0; i<120; i++)
		dir.fNewFile();
	console.log(dir._dirRst);
dir_close();
}

//-打开目录索引库-
function test_dir_open()
{
dir_open();
	for(var i=0; i<11000; i++)
		dir.fNewFile();
	console.log(dir._dirRst);
dir_close();
}


//-----------------------------------
//			test filedb 
//-----------------------------------
//-正式测试:新建并添加-
function test_db_new()
{
db_open();
	var data;
	for(var i=0; i<204; i++)
	{
		data = "src"+i+".txt";
		db.fSave(data, "src", i, "txt");
		console.log("------------------------------");
	}
db_close();
}

//-正式测试:打开并检索-
function test_db_open()
{
db_open();
	console.log(db.fGet( "src", 102, "txt"));
db_close();
}

//-正式测试:打开并删除添加-
function test_db_del()
{
db_open();
	db.fDel("src", 102, "txt");
	console.log(db.fGet( "src", 102, "txt"));
db_close();
}


//-----------------------------------
//			test main
//-----------------------------------
function test(n)
{
	switch(n)
	{
		case 10:	test_file_new(); break;
		case 11:	test_file_open(); break;
		case 20:	test_dir_new(); break;
		case 21:	test_dir_open(); break;
		case 30:	test_db_new(); break;
		case 31:	test_db_open(); break;
		case 32:	test_db_del(); break;

		case 90:	test_isFileExist();	break;
		case 91:	test_createDir();	break;
		case 92:	test_createSubDir();	break;
	}	
}

test(30);

